from django import template
from django.template import resolve_variable
register = template.Library()

class FormatDictionary(template.Node):
	def __init__(self, dictionary, list_position, key):
		self.list_position = list_position
		self.dictionary = dictionary
		self.key = key
		
	def render(self, context):
		try:
			di = resolve_variable(self.dictionary, context)
			ke = resolve_variable(self.key, context)
			li = resolve_variable(self.list_position, context)
			return str(di[ke][li])
		except VariableDoesNotExist:
			return ''

@register.tag
def dictionary_grab(parser, token):
	tag_name, dictionary, key, list_position = token.split_contents()
	return FormatDictionary(dictionary, list_position, key)